#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iterator>
using namespace std;
class Solution
{
public:
    /**
     * @brief 1 <= s.length <= 2000
     *
     * @param s
     * @return int
     */
    int longestPalindrome(string s)
    {
        unordered_map<char, int> ans;
        for (char c : s)
        {
            ans[c]++;
        }
        int sum = 0;
        for (auto &v : ans)
        {
            sum += v.second / 2 * 2;
            // center
            if (v.second % 2 == 1 && sum % 2 == 0)
            {
                sum++;
            }
        }
        return sum;
    }
    int longestPalindrome_2(string s)
    {
        vector<int> m(128, 0);
        int len = 0;
        for (auto c : s)
        {
            if (m[c] & 1)
            {
                len += 2;
            }
            m[c]++;
        }
        if (len < s.size())
        {
            len++;
        }
        return len;
    }
};
int main()
{
    Solution s;
    cout << s.longestPalindrome("bananas") << endl;
    system("pause");
    return 0;
}
